var args = arguments[0] || {};

/*
 *
 * Read json manifest and create tableviewcell
 *
 */
var readContents;
var readFile = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, '/modules/lapostemanifestapp.json');

if (readFile.exists()) {
	readContents = readFile.read();

	var doc = readContents.text;
	var jsonObject = JSON.parse(doc);
	var manifest = jsonObject.manifest;
	var allMenus = manifest.menu;
	var menus = [];
	var data = [];
	var countAllMenus = allMenus.length;

	for (var i = 0; i < countAllMenus; i++) {
		var item = allMenus[i];
		if (item.level == "0") {
			menus.push(item);
		}
	}

	/*
	 *
	 *  Create tableviewcell
	 */
	Ti.API.info('num cell: ' + menus.length);

	var countMainMenu = menus.length;
	for (var i = 0; i < countMainMenu; i++) {
		var menuInfo = menus[i];
		var row = Titanium.UI.createTableViewRow();
		var item = menuInfo.item;
		var rowTitle = item.title[0];
		Ti.API.info('Menu Info: ' + rowTitle.text);

		//row.title = rowTitle.text;
		var customView = Ti.UI.createView({
			left : 0,
			top : 0,
			height : 80
		});

		var imgView = Ti.UI.createImageView({
			left : 10,
			top : 10,
			width : 60,
			height : 60,
			image : '/images/' + menuInfo.icone
		});
		customView.add(imgView);

		var label = Ti.UI.createLabel({
			left : 80,
			height : 30,
			top : 25,
			width : 230,
			color : '#333333'
		});
		label.text = rowTitle.text;
		label.font = {
			fontFamily : 'DINPro-Regular',
			fontSize : '20'
		};
		customView.add(label);

		if (rowTitle.text == 'Autres applications' || rowTitle.text == 'Mentions légales') {
			customView.backgroundColor = '#dddedb';
		}

		// add click menu item
		customView.addEventListener("click", function(_event) {

			// sub menu web view
			if (menuInfo.url.text != '') {
				var args = [];
				args.push({
					name : 'url',
					value : menuInfo.appid.text + "/" + menuInfo.url.text
				});
				args.push({
					name : 'color',
					value : menuInfo.color.text
				});
				args.push({
					name : 'title',
					value : menuInfo.item.title[0].text.text
				});

				var subMenuWebView = Alloy.createController('subMenuWebView', args).getView();
				subMenuWebView.open();
			} else if (rowTitle.text == 'Autres applications' || rowTitle.text == 'Mentions légales') {
				// 2 ngoai le

			} else {
				// sub menu
			}

		});

		// Add custom view
		row.add(customView);
		data.push(row);
	}

	$.mainMenuTable.setData(data);
}

if (!Ti.Platform.Android) {
	$.mainMenuTable.setSeparatorInsets({
		left : 0,
		right : 0
	});
}
